package com.situ.company.util;

import java.sql.Statement;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;


public class JDBCUtil {
	private static ResourceBundle rb = ResourceBundle.getBundle("com.situ.company.util.jdbcutil");

	private static String driver = rb.getString("driver");
	private static String url = rb.getString("url");
	private static String user = rb.getString("user");
	private static String pass = rb.getString("password");

	private JDBCUtil() {

	}

	static {
		// 驱动
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		try {
			return DriverManager.getConnection(url, user, pass);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 关闭
	public static void close(Connection conn, Statement st, ResultSet rs) {
		try {// 查
			if (st != null) {
				st.close();
				st = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
			if (rs != null) {
				rs.close();
				rs = null;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void close(Connection conn, Statement st) { // Statement 更好，PreparedStatement可以向上转型
		close(conn, st, null);// 增删改
	}

	public static void main(String[] args) {

	}

	public static Integer update(String sql, Object... obj) {
		
		return update(sql, Arrays.asList(obj));
		// obj数组中元素类型是引用类型 -> asList返回的集合中存放的元素是obj数组中的每个元素
		// obj数组中元素类型是基本数据类型 -> asList返回的集合中存放的元素仅有一个，即obj这个数组作为list集合中的这个元素
	}
	public static Integer update(String sql, List<Object> obj) {
		Connection con =null;
		PreparedStatement ps= null;
		
		try {
			con = getConnection();
			ps=con.prepareStatement(sql);
			for(int i=0;i<obj.size();i++) {
				ps.setObject(i+1, obj.get(i));
			}
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close(con,ps);
		}	
		return null;
		
	}
	
	/**
	 * @param <T>
	 * @param sql
	 * @param obj
	 * @param clazz
	 * 			返回的集合中的元素类型
	 * @param fields
	 * 			key=属性名 value=字段名
	 * @return
	 */
	public static <T> List<T> query(String sql, List<Object> obj,Class<T> clazz,Map<String,String> fields) {
		Connection con =null;
		PreparedStatement ps= null;
		ResultSet rs = null;
		List<T> result = new ArrayList<>();
		try {
			con = getConnection();
			ps=con.prepareStatement(sql);
			for(int i=0;i<obj.size();i++) {
				ps.setObject(i+1, obj.get(i));
			}
			rs = ps.executeQuery();
			while(rs.next()) {
				T model = clazz.newInstance();
				for(Entry<String,String> entry : fields.entrySet()) {
				 	Field field = clazz.getDeclaredField(entry.getKey());
				 	field.setAccessible(true);
				 	field.set(model, rs.getObject(entry.getValue()));
				}
				result.add(model);
			}
		} catch (SQLException | InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
			e.printStackTrace();
		}finally {
			close(con,ps,rs);
		}	
		
		return result;
	}
	
	public static Integer queryInt(String sql, List<Object> obj) {
		Connection con =null;
		PreparedStatement ps= null;
		ResultSet rs = null;
		try {
			con = getConnection();
			ps=con.prepareStatement(sql);
			for(int i=0;i<obj.size();i++) {
				ps.setObject(i+1, obj.get(i));
			}
			rs = ps.executeQuery();
			while(rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close(con,ps,rs);
		}		
		return null;
	}
}
